package com.tuanzi.controller.Pot;

import com.tuanzi.ResultVo.ResultVO;
import com.tuanzi.entity.FlowerPot;
import com.tuanzi.entity.User;
import com.tuanzi.service.IndexService;
import com.tuanzi.service.PotService;
import com.tuanzi.utils.Status;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author tuanzi
 * @version 1.0
 * @date 2022/4/4 17:03
 */

@RestController
public class PotController {

    /**
    * 对花盆进行增删改查等操作
    */
    @Resource
    private PotService potService;

    @Resource
    private IndexService indexService;

    // 添加新的花盆 -- 这个只是限于网站添加
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultVO addPot(@RequestParam String name, @RequestParam String description, HttpSession session){
        System.out.println("我被调用了======================");
        ResultVO resultVO;
        System.out.println("我被调用了======================");
        User user = (User) session.getAttribute("user");
        System.out.println("获取到的用户id："+user.getId());
        // 添加花盆
        FlowerPot pot = potService.addFlowerPot(user.getId(), name, description);
        if (pot != null){
            // 添加成功 -- 更新一下session -- 获取所有的花盆列表，然后更新
            List<FlowerPot> flowerPots = indexService.getUserFlowerPots(user.getId());
            user.setFlowerPotList(flowerPots);
            session.setAttribute("user",user);
             resultVO = new ResultVO(Status.ADDSUCCESS,user);
        }else {
            resultVO = new ResultVO(Status.ADDFAILED,null);
        }
        return resultVO;
    }


    // 删除花盆
    @RequestMapping("/del")
    public ResultVO deletePot(@RequestParam Integer id){

        // 根据删除的id，删除所有的温度和湿度值

        ResultVO vo = new ResultVO(Status.DELETEFAILED);

        FlowerPot pot = potService.deleteFlowerPot(id);

        if (pot != null){
            // 删除成功
            // 如果删除失败，那么状态不进行任何变化，还是FAILED
            vo.setStatus(Status.DELETESUCCESS);
        }

        return vo;
    }


}
